package com.yunwushanhe.handler.exception;

import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.enums.AppHttpCodeEnum;
import com.yunwushanhe.exception.BlogSystemException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author 云雾山河
 * @Date 2023/8/10 14:41
 * 全局异常处理器处理全局异常
 */
@Slf4j
@RestControllerAdvice // 由try catch处理的异常不会经过这个处理器处理，throw会，偶然发生的异常也会
public class GlobalExceptionHandler {

    @ExceptionHandler(BlogSystemException.class)
    public ResponseResult<Object> blogSystemExceptionHandler(BlogSystemException e){
        log.error("全局异常处理器发现系统异常: {}",e.getMsg());
        e.printStackTrace();
        return ResponseResult.error(e.getCode(),e.getMsg());
    }

    /**
     * 处理其他未知异常
     */
    @ExceptionHandler(Exception.class)
    public ResponseResult<Object> exceptionHandler(Exception e){
        log.error("全局异常处理器发现异常: {}",e.toString());
        e.printStackTrace();
        return ResponseResult.error(AppHttpCodeEnum.SYSTEM_ERROR.getCode(),e.getMessage());
    }
}
